layui.use(['form', 'admin', 'ax', 'upload'], function () {
    var $ = layui.jquery;
    var $ax = layui.ax;
    var form = layui.form;
    var admin = layui.admin;

    // 单位类型
    var contactTypeAjax = new $ax(Changein.ctxPath + "/contactType/listContactType", function (data) {
        if (data.code == 200 && data.data.length > 0) {
            $.each(data.data, function (n, value) {
                $("#contactTypeId").append('<option value="' + value.id + '">' + value.name + '</option>');
            });
            form.render();
        }
    });
    contactTypeAjax.start();

    // 获取选中的单位类型
    form.on('select(contactTypeId)', function (data) {
        if (data.value) {
            var selectText = data.elem[data.elem.selectedIndex].text;
            $("#contactType").val(selectText);
        } else {
            $("#contactType").val('');
        }
    });

    //表单提交事件
    form.on('submit(btnSubmit)', function (data) {
        data.field.state = data.field.state == 'on' ? 1 : 0;
        var ajax = new $ax(Changein.ctxPath + "/client/addItem", function (data) {
            if (data.code == 200) {
                Changein.success("添加成功！");
                //传给上个页面，刷新table用
                admin.putTempData('formOk', true);
                //关掉对话框
                admin.closeThisDialog();
            } else {
                Changein.error("添加失败！" + data.message)
            }
        }, function (data) {
            Changein.error("添加失败！" + data.responseJSON.message)
        });
        ajax.set(data.field);
        ajax.start();
        return false;
    });

});